package com.example.hxpc.guidancemanagement.tools.dataTools;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.example.hxpc.guidancemanagement.api.BaseApi;
import com.example.hxpc.guidancemanagement.model.ApiInfo;
import com.example.hxpc.guidancemanagement.model.JsonBase;
import com.example.hxpc.guidancemanagement.model.JsonFeedback;
import com.example.hxpc.guidancemanagement.tools.LogoutT;
import com.example.hxpc.guidancemanagement.tools.gson.GsonUtil;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.util.Map;

import okhttp3.Call;

import static com.zhy.http.okhttp.OkHttpUtils.post;

/**
 * Created by tao on 2017/11/26.
 * 网络请求
 */

public class OkHttpTools {
    private BaseApi baseApi;

    /**
     * 登录
     * @param telephone 手机
     * @param password 密码
     * @param flg 类型：1-管理员，2-超级用户，3-普通用户
     */
    public void login(final BaseApi baseApi , Context context , String telephone , String password , String flg){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.loginUrl)
                .addParams("telephone",telephone)
                .addParams("password",password)
                .addParams("flg",flg)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogoutT.logout(response);
                        JsonBase loginM = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                        baseApi.backMsg(loginM.getSuccess(),loginM);

                    }
                });
    }


    /**
     * 用户注册信息
     * @param context
     * @param name
     * @param telephone
     * @param password
     */
    public void register(final BaseApi baseApi , Context context , String name , String telephone , String password){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.registerUrl)
                .addParams("name",name)
                .addParams("password",password)
                .addParams("telephone",telephone)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        JsonBase registerM = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                        baseApi.backMsg(registerM.getSuccess(),registerM);
                    }
                });
    }

    /**
     * 修改用户昵称
     * @param baseApi
     * @param uid
     * @param name
     */
    public void changeName(final BaseApi baseApi , String uid , String name){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.changeNameUrl)
                .addParams("uid",uid)
                .addParams("name",name)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        JsonBase changeNameM = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                        baseApi.backMsg(changeNameM.getSuccess(),changeNameM);
                    }
                });
    }

    /**
     * 修改用户的手机号
     * @param baseApi
     * @param uid
     * @param telephone
     */
    public void changeUserPhone(final BaseApi baseApi , String uid , String telephone){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.changePhoneUrl)
                .addParams("uid",uid)
                .addParams("telephone",telephone)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        JsonBase changePhoneM = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                        baseApi.backMsg(changePhoneM.getSuccess(),changePhoneM);
                    }
                });
    }

    /**
     * 忘记密码找回密码
     * @param baseApi
     * @param telephone
     * @param password
     */
    public void forgetPassword(final BaseApi baseApi , String telephone , String password){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.forgetPasswordUrl)
                .addParams("telephone",telephone)
                .addParams("password",password)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        JsonBase forgetPassword = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                        baseApi.backMsg(forgetPassword.getSuccess(),forgetPassword);
                    }
                });
    }

    /**
     * 判断该手机号是否注册过
     * @param baseApi
     * @param telephone
     */
    public void isExitTelephone(final BaseApi baseApi , String telephone){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.isExitTelephoneUrl)
                .addParams("telephone",telephone)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        JsonBase isExitTelephone = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                        baseApi.backMsg(isExitTelephone.getSuccess(),isExitTelephone);
                    }
                });
    }

    /**
     * 修改密码
     * @param baseApi
     * @param telephone
     * @param password
     */
    public void changePassword(final BaseApi baseApi , String telephone , String password){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.changePasswordUrl)
                .addParams("telephone",telephone)
                .addParams("password",password)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        JsonBase changePassword = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                        baseApi.backMsg(changePassword.getSuccess(),changePassword);
                    }
                });

    }

    /**
     * 上传头像API
     * @param baseApi
     * @param telephone
     * @param file
     */
    public void uploadPhoto(final BaseApi baseApi , String telephone , File file){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.uploadHeadPhotoUrl)
                .addParams("telephone",telephone)
                .addFile("avatar","avatar.jpg",file)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        JsonBase uploadPhoto = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                        baseApi.backMsg(uploadPhoto.getSuccess(),uploadPhoto);
                    }
                });
    }

    /**
     * 创建项目API
     * @param baseApi
     * @param projectName
     * @param join_num
     * @param teacher
     * @param state
     */
    public void createProject(final BaseApi baseApi , String projectName , String join_num , String teacher , String state ,String path){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.createProjectUrl)
                .addParams("projectName",projectName)
                .addParams("join_num",join_num)
                .addParams("teacher",teacher)
                .addParams("state",state)
                .addParams("path",path)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        LogoutT.logout("createProject"+e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogoutT.logout("createProject"+response);
                        JsonBase createProject = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                        baseApi.backMsg(createProject.getSuccess(),createProject);
                    }
                });
    }

    /**
     * 获取项目列表
     * @param baseApi
     */
    public void getAllProject(final BaseApi baseApi){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.getAllProjectUrl)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        LogoutT.logout("getAllProject"+e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogoutT.logout("成功ccc  "+response);
                        JsonFeedback projects = GsonUtil.getIns().parseJsonWithGson(response,JsonFeedback.class);
                        baseApi.backMsg(projects.getSuccess(),projects);
                    }
                });
    }

    /**
     * 获取所有的成员接口
     * @param baseApi
     */
    public void getAllNumber(final BaseApi baseApi){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.getAllNumberUrl)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        LogoutT.logout("getAllNumber:  "+e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogoutT.logout("getAllNumber:  "+response);
                        JsonFeedback getAllNumber = GsonUtil.getIns().parseJsonWithGson(response,JsonFeedback.class);
                        baseApi.backMsg(getAllNumber.getSuccess(),getAllNumber);
                    }
                });
    }

    /**
     * 删除某一位成员
     * @param baseApi
     * @param telephone
     */
    public void removeNumber(final BaseApi baseApi , String telephone){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.removeNumberUrl)
                .addParams("telephone",telephone)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        LogoutT.logout("removeNumber"+e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogoutT.logout("removeNumber"+response);
                        JsonBase removeN = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                        baseApi.backMsg(removeN.getSuccess(),removeN);
                    }
                });
    }

    /**
     * 获取所有优秀项目
     * @param baseApi
     */
    public void getGoodProject(final BaseApi baseApi){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.getGoodProjectUrl)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        LogoutT.logout("getGoodProject"+e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogoutT.logout("getGoodProject"+response);
                        LogoutT.logout("getGoodProject"+id);
                        JsonFeedback getGP = GsonUtil.getIns().parseJsonWithGson(response,JsonFeedback.class);
                        baseApi.backMsg(getGP.getSuccess(),getGP);
                    }
                });
    }

    /**
     * 获取所有的优秀人物
     * @param baseApi
     */
    public void getGoodPerson(final BaseApi baseApi){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.getGoodPersonUrl)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        LogoutT.logout("goodPerson"+e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogoutT.logout("goodPerson"+response);
                        JsonFeedback getGP = GsonUtil.getIns().parseJsonWithGson(response,JsonFeedback.class);
                        baseApi.backMsg(getGP.getSuccess(),getGP);
                    }
                });
    }

    /**
     * 活动模块图片上传
     * @param baseApi
     * @param content
     * @param file
     */
    public void publishActivityInfo(final BaseApi baseApi, String content , Map<String,File> file,String publishTime,String useHeadPhoto,String peopleName){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.publishActivityInfoUrl)
                .addParams("content",content)
                .files("contentPic",file)
                .addParams("publishTime",publishTime)
                .addParams("useHeadPhoto",useHeadPhoto)
                .addParams("peopleName",peopleName)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        LogoutT.logout("uploadPhotos    "+e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogoutT.logout("uploadPhotos    "+response);
                        JsonBase activityInfo = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                        baseApi.backMsg(activityInfo.getSuccess(),activityInfo);
                    }
                });

    }

    /**
     * 只发表文字的活动信息
     * @param baseApi
     * @param content
     */
    public void onlyPublishText(final BaseApi baseApi , String content,String publishTime ,String useHeadPhoto,String peopleName){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.onlyPublishActivityInfoUrl)
                .addParams("content",content)
                .addParams("publishTime",publishTime)
                .addParams("useHeadPhoto",useHeadPhoto)
                .addParams("peopleName",peopleName)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        LogoutT.logout("onlyPublishText"+e );
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogoutT.logout("onlyPublishText"+response);
//                        JsonFeedback onlyPT = GsonUtil.getIns().parseJsonWithGson(response,JsonFeedback.class);
//                        baseApi.backMsg(onlyPT.getSuccess(),onlyPT);
                        JsonBase onlyPT = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                        baseApi.backMsg(onlyPT.getSuccess(),onlyPT);
                    }
                });
    }

    /**
     * 获取所有的活动信息
     * @param baseApi
     */
    public void getActivityInformation(final BaseApi baseApi ){
        this.baseApi = baseApi;
        post()
                .url(ApiInfo.getActivityInformationUrl)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        LogoutT.logout("getActivityInformation"+e );
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogoutT.logout("getActivityInformation"+response);
                        JsonFeedback getAI = GsonUtil.getIns().parseJsonWithGson(response,JsonFeedback.class);
                        baseApi.backMsg(getAI.getSuccess(),getAI);
                    }
                });
    }

    /**
     * 活动留言
     * @param baseApi
     * @param name
     * @param content
     * @param act_id
     */
    public void leaveMessage(String name ,String content ,String act_id){
        post()
                .url(ApiInfo.leaveMessageUrl)
                .addParams("name",name)
                .addParams("content",content)
                .addParams("act_id",act_id)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        LogoutT.logout("leaveMessage"+ e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogoutT.logout("leaveMessage"+ response);
                    }
                });
    }

}
